/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.plugin.tpch;

import com.google.common.collect.AbstractIterator;
import io.trino.spi.Page;
import io.trino.spi.connector.ConnectorPageSource;
import io.trino.spi.connector.RecordPageSource;
import io.trino.spi.predicate.TupleDomain;
import io.trino.spi.type.Type;
import io.trino.tpch.TpchTable;

import java.util.Iterator;
import java.util.List;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.tpch.TpchRecordSet.createTpchRecordSet;

public final class TpchTables
{
    private TpchTables()
    {
    }

    public static List<Type> getTableColumns(String tableName, DecimalTypeMapping decimalTypeMapping)
    {
        TpchTable<?> table = TpchTable.getTable(tableName);
        return table.getColumns().stream()
                .map(column -> TpchMetadata.getTrinoType(column, decimalTypeMapping))
                .collect(toImmutableList());
    }

    public static Iterator<Page> getTablePages(
            String tableName,
            double scaleFactor,
            DecimalTypeMapping decimalTypeMapping)
    {
        TpchTable<?> table = TpchTable.getTable(tableName);
        ConnectorPageSource pageSource = new RecordPageSource(
                createTpchRecordSet(table, decimalTypeMapping, scaleFactor, 1, 1, TupleDomain.all()));
        return new AbstractIterator<>()
        {
            @Override
            protected Page computeNext()
            {
                if (pageSource.isFinished()) {
                    return endOfData();
                }

                Page page = pageSource.getNextPage();
                if (page == null) {
                    return computeNext();
                }

                return page.getLoadedPage();
            }
        };
    }
}
